package com.example.service.user.controller;


import com.example.common.ClientType;
import com.example.common.ResponseVO;
import com.example.common.route.RouteHandle;
import com.example.common.route.RouteInfo;
import com.example.common.utils.RouteInfoParseUtil;
import com.example.service.user.model.req.*;
import com.example.service.user.service.ImUserService;
import com.example.service.user.service.ImUserStatusService;
import com.example.service.utils.ZKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("v1/user")
@Api(tags = "用户相关接口")
public class ImUserController {

    @Autowired
    ImUserService imUserService;

    @Autowired
    ZKit zKit;

    @Autowired
    RouteHandle routeHandle;


    @Autowired
    ImUserStatusService imUserStatusService;


    @PostMapping("importUser")
    @ApiOperation(value = "导入用户")
    public ResponseVO importUser(@RequestBody @Validated ImportUserReq req){
        return imUserService.importUser(req);
    }

    @DeleteMapping("/deleteUser")
    @ApiOperation(value = "删除用户")
    public ResponseVO deleteUser(@RequestBody @Validated DeleteUserReq req) { //,Integer appId
//        req.setAppId(appId);
        return imUserService.deleteUser(req);
    }

    @PostMapping("/login")
    @ApiOperation(value = "设置用户登入")
    public ResponseVO login(@RequestBody @Validated LoginReq req){
        ResponseVO login = imUserService.login(req);
        if (login.isOk()){
            //去zk里面获取一个im的地址  返回到sdk
            List<String> allWebNode=new ArrayList<>();
            if (req.getClientType()== ClientType.WEB.getCode()){
                allWebNode=zKit.getAllWebNode();
            }else {
                allWebNode=zKit.getAllTcpNode();
            }
            //ip:port
            String s = routeHandle.routeServer(allWebNode, req.getUserId());
            RouteInfo parse = RouteInfoParseUtil.parse(s);
            return ResponseVO.successResponse(parse);

        }
        return ResponseVO.errorResponse();
    }

    @GetMapping("/getUserSequence")
    @ApiOperation(value = "获取用户seq来进行判断需不需要进行增量同步")
    public ResponseVO getUserSequence( @Validated GetUserSequenceReq req) {
        return imUserService.getUserSequence(req);
    }

    @PostMapping("/subscribeUserOnlineStatus")
    @ApiOperation(value = "临时订阅用户的状态")
    public ResponseVO subscribeUserOnlineStatus(@RequestBody @Validated
                                                SubscribeUserOnlineStatusReq req) {
        imUserStatusService.subscribeUserOnlineStatus(req);
        return ResponseVO.successResponse();
    }


    @PostMapping("/setUserCustomerStatus")
    @ApiOperation(value = "进行设置用户的状态")
    public ResponseVO setUserCustomerStatus(@RequestBody @Validated
                                            SetUserCustomerStatusReq req) {
//        req.setAppId(appId);
//        req.setOperater(identifier);
        imUserStatusService.setUserCustomerStatus(req);
        return ResponseVO.successResponse();
    }


    @PostMapping("/queryFriendOnlineStatus")
    @ApiOperation(value = "查询好友在线状态")
    public ResponseVO queryFriendOnlineStatus(@RequestBody @Validated
                                              PullFriendOnlineStatusReq req) {
//        req.setAppId(appId);
//        req.setOperater(identifier);
        return ResponseVO.successResponse(imUserStatusService.queryFriendOnlineStatus(req));
    }

    @PostMapping("/queryUserOnlineStatus")
    @ApiOperation(value = "查询指定用户在线状态")
    public ResponseVO queryUserOnlineStatus(@RequestBody @Validated
                                            PullUserOnlineStatusReq req) {
        return ResponseVO.successResponse(imUserStatusService.queryUserOnlineStatus(req));
    }




}
